/*
 * 用户反馈 查看详情
 */

$(function(){
	//获取url中的数据
	var LocString=String(window.document.location.href);
	function GetQueryString(str){
		var rs=new RegExp("(^|)"+str+"=([^&]*)(&|$)","gi").exec(LocString),tmp;
		if(tmp=rs)return tmp[2];
		return "没有这个参数";
	}

	var page;
	var startTime=GetQueryString("st");
	var endTime=GetQueryString("eT");
	var adId = GetQueryString("adId");
	var praiseNumPer=GetQueryString("praiseNumPer")
	var stepNumPer=GetQueryString("stepNumPer");
	var adName=$("#adName").val();
	index(1,null);
	
	$("#search").on("click",function(){
		back();
	});
	/**********************分页***********************/
	function ByAdNameTwo(page){
		$('#byadName tr td').remove();
		if(endTime&&startTime){
		$.ajax({
			url : '/Lvscore_Advertiser_Maven/feedbackAdCount',
	        data:{"startTime":startTime,"endTime":endTime,"page":page,"adId":adId},
	        dataType:'json',
			type : 'post',
			success : function(data) {
				
				var commentTotal=0;
				var complainTotal=0;
				var obj=data.beans;
				var totalComments=0;
				var tbody='';
				
				for(var i=0;i<obj.length;i++){
					if(obj[i].complain==null){
						obj[i].complain=0;
					}
					if(obj[i].commentNum==null){
						obj[i].commentNum=0;
					}
					if(obj[i].adType =="video"){
						var viewadUrl = "/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/vedioAd/queryVedioAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;
					}else{
						var viewadUrl = "/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/pictureAd/queryPictureAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;

					}
					tbody+='<tr>\
						<td>'+(i+1)+'</td>\
					 	<td>'+translate(obj[i].createTime)+'</td>\
					 	<td>'+obj[i].adName+'</td>\
					 	<td><a href='+viewadUrl+'>'+obj[i].adCode+'</a></td>\
					 	<td>'+transitionAdType(obj[i].adType)+'</td>\
					 	<td>'+transitionTerminalTyp(obj[i].terminalType)+'</td>\
					 	<td class="commentNum">'+obj[i].commentNum+'</td>\
					 	<td>'+obj[i].complain+'</td>\
					 </tr>';
					commentTotal+=parseInt(obj[i].commentNum);
					complainTotal+=parseInt(obj[i].complain);
				}
					
				$("#byadName").append(tbody);
				if($(".commentNums").html()==0){
					alert("该广告名下搜索不到哦")
				}
				var tbodyTwo="<tr>" +
					"<td>总计</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>"+commentTotal+"</td>" +
					"<td>"+complainTotal+"</td></tr>";
					$('#byadName').append(tbodyTwo);
			},
	        error: function(){}             
		});
		}
	}


	/**********************over***********************/

	/**
	 * 转换时间为日期
	 * **/
	 function translate(t){
			 t = t.split(" ")[0];	
			 t=t.replace("-",".");
			 t=t.replace("-",".");
			 return t;
	};
	
	/**
	 * 转换  广告类型
	 */
	function transitionAdType(adType){
		switch (adType){
		   case "video":
			  adType ="视频";
		     break;
		   case "picture":
			  adType ="图片";
		     break;
		   default:
		     adType ="";
		}
		
		return adType;
	}
	
	/**
	 * 转换  mobile，pc，pad，tv
	 */
	function transitionTerminalTyp(terminalType){
		switch (terminalType) {
		   case "mobile":
			   terminalType ="手机";
		     break;
		   case "pc":
			   terminalType =" PC";
		     break;
		   case "pad":
			   terminalType ="PAD";
		     break;
		   case "tv":
			   terminalType ="互联网电视";
		     break;
		   default:
			   terminalType ="";
		}
		
		return terminalType;
	};
	
	function back(){
		var startTime=$("#startDay").val();
		var endTime=$("#endDay").val();
		var adName = $("#adName").val();
		window.location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/feedback/index.html?selectedNav=5&navPos=0&startTime="+startTime+"&endTime="+endTime+"&adName="+adName;
		
	};
	
	function index(page,adName){
		
		$("#startDay").val(startTime);
		$("#endDay").val(endTime);
		$("#goodrate").html(praiseNumPer);
		$("#badrate").html(stepNumPer);
		var initTable='<tr>\
		     	<th>序号</th>\
		     	<th>时间</th>\
		     	<th>广告名称</th>\
		     	<th>广告编码</th>\
		     	<th>投放形式</th>\
		     	<th>投放终端</th>\
				<th>评论数</th>\
				<th>投诉</th>\
			</tr>';
		
		$('#byadName').html(initTable);
		if(page==null || page.length<=0){
			page=1;
		}
		$.ajax({
			url : '/Lvscore_Advertiser_Maven/feedbackAdCount',
	        data:{"startTime":startTime,"endTime":endTime,"page":page,"adId":adId},
			type : 'post',
			dataType:"json",
			success : function(data) {
				var commentTotal=0;
				var complainTotal=0;
				var obj=data.beans;
				var totalComments=0;
				var tbody='';
				
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	ByAdNameTwo(page);	
		            }
				});
				$("#adName").val(obj[0].adName)
				for(var i=0;i<obj.length;i++){
					if(obj[i].complain==null){
						obj[i].complain=0;
					}
					if(obj[i].commentNum==null){
						obj[i].commentNum=0;
					}
					if(obj[i].adType =="video"){
						var viewadUrl = "/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/vedioAd/queryVedioAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;
					}else{
						var viewadUrl = "/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/pictureAd/queryPictureAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;

					}

					tbody+='<tr>\
						<td>'+(i+1)+'</td>\
					 	<td>'+translate(obj[i].createTime)+'</td>\
					 	<td>'+obj[i].adName+'</td>\
					 	<td><a href='+viewadUrl +'>'+obj[i].adCode+'</a></td>\
					 	<td>'+transitionAdType(obj[i].adType)+'</td>\
					 	<td>'+transitionTerminalTyp(obj[i].terminalType)+'</td>\
					 	<td class="commentNum">'+obj[i].commentNum+'</td>\
					 	<td>'+obj[i].complain+'</td>\
					 </tr>';
					commentTotal+=parseInt(obj[i].commentNum);
					complainTotal+=parseInt(obj[i].complain);
				}
					
				$("#byadName").append(tbody);
				if($(".commentNums").html()==0){
					alert("该广告名下搜索不到哦")
				}
				var tbodyTwo="<tr>" +
					"<td>总计</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>"+commentTotal+"</td>" +
					"<td>"+complainTotal+"</td></tr>";
					$('#byadName').append(tbodyTwo);
			},
	        error: function(){}        
		});
	};
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	};
	function total(page){
		if(endTime&&startTime){
			$.ajax({
				url : '/Lvscore_Advertiser_Maven/feedbackAdCount',
		        data:{"startTime":startTime,"endTime":endTime,"adId":adId,"page":page},
		        dataType:'json',
				type : 'post',
				success : function(data) {
					var commentTotal=0;
					var complainTotal=0;
					var obj=data.beans;
					var totalComments=0;
					var tbody='';
					
					$('#pageBox').extendPagination({
						totalCount:data.total,
				 		showPage: data.pages+1,
			            limit: data.pageSize,
			            goPage:page,
			            callback: function (page) {
			            	ByAdNameTwo(page);	
			            }
					});
				},
		        error: function(){}        
			});
		  }
	};
	//分页跳转
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
				
			}else{
				if(page==1){
					 index(page,null);
				}else if(page>showPageN-4){
					
				}else{
					total(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
	
});



